package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.Merchant;
import com.turing.post.organization.util.MerchantDao;
import com.turing.post.transaction.util.MchTradDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 规则： 对于N天内无交易的渠道，系统即进行风险预警
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule12")
public class AlarmRule12 implements AlarmRulesBusiness {
	

	@PersistenceContext
	protected EntityManager _em;
	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "organization.impl.MerchantDaoImpl")
	private MerchantDao merchantDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;

	@EJB(mappedName = "transaction.impl.MchTradDetailDaoImpl")
	private MchTradDetailDao mchTradDetailDaoImpl;

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
		IssueCompany issueCompany = issueCompanyDaoImpl.getIssueCompanyByIssueCompanyNo(issueCompanyNo);
		String parameter[] = alarmRule.getRuleParameter().split(":");
		if(parameter.length > 0){
			//如果预警参数不是数字，记录日志，直接反回
			if(!PCPSUtil.isNumeric(parameter[0])){
				PCPSUtil.Log("渠道预警规则【" + alarmRule.getAlarmRuleDesc() + "】的预警参数设置错误，参数应该是数字。");
				return;
			}
			//获取渠道交易
			List<Object[]> objList = mchTradDetailDaoImpl.getMchDealSum(tranDate, Integer.parseInt(parameter[0].toString()), issueCompanyNo);
			for (Object[] obj : objList) {
				if(Integer.parseInt(obj[1].toString()) <= 0){
					Merchant merchant = merchantDaoImpl.getMerchantByMerchantNo(obj[0].toString());
					releaseAlarm(merchant, issueCompany, alarmRule, tranDate);
				}
			}
		}else{
			PCPSUtil.Log("渠道预警规则【" + alarmRule.getAlarmRuleDesc() + "】的预警参数未设置。");
			return;
		}
	}
	
	/**
	 * 发布风险信息
	 * 
	 * @param tranDate
	 * @param alarmRule
	 * @param issueCompany
	 * 
	 * @param alarmOrders存在风险的销售单
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(Merchant merchant, IssueCompany issueCompany, AlarmRule alarmRule, Date tranDate) throws PCPSApplicationException {
		AlarmInfo alarmInfo = new AlarmInfo();
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompany.getIssueCompanyNo());
		alarmInfo.setBranchNo(merchant.getMerchantNo());
		alarmInfo.setBranchName(merchant.getMerchantName());
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		alarmInfo.setRiskCustomName(merchant.getMerchantName());
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus("0");

		BigInteger alarmInfoId = alarmInfoDaoImpl.getMaxAlarmInfoId();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		// 插入预警信息
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
	}

}
